﻿#include "TomatoClockWidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QProgressBar>
#include <QTimer>
#include <QTime>
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include <qinputdialog.h>
#include "TomatoConfigDialog.h"
#include "toolBox.h"
TomatoClockWidget::TomatoClockWidget(QWidget* parent)
    : QWidget(parent)
    , m_isWorking(true)
    , is_contiune(true)
    , m_completedTomatoes(0)
    , m_progressValue(0)
    , CustomRestTimer(QTime(0,5,0)) // 25分钟
    , CustomWorkTimer(QTime(0,45,0))
{
    m_completedTomatoes = toolBox::getInstance().softwareConfiger.findSaveConfig("tomato_goal").toInt();
    setupUI();
    setupAnimations();
    m_trayIcon=new QSystemTrayIcon(this);
    m_trayIcon->setIcon(QIcon("C:\\Users\\xiejun\\Desktop\\qplayter\\qGameVideo\\resource\\black.png")); // 确保路径正确
    m_trayIcon->show(); // 显示托盘图标

  
    m_timer = new QTimer(this);
    m_db=new SqlLite();
    connect(m_timer, &QTimer::timeout, this, &TomatoClockWidget::updateTimer);
    m_remainingTime = CustomWorkTimer; // 25分钟
    resetTimer();

}

void TomatoClockWidget::setupUI()
{
    // 主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(30, 30, 30, 30);

    // 标题
    QLabel* titleLabel = new QLabel("🍅 番茄工作法");
    titleLabel->setObjectName("titleLabel");
    titleLabel->setAlignment(Qt::AlignCenter);

    // 时间显示
    m_timeLabel = new QLabel("25:00");
    m_timeLabel->setObjectName("timeLabel");
    m_timeLabel->setAlignment(Qt::AlignCenter);

    // 状态标签
    m_statusLabel = new QLabel("工作时间");
    m_statusLabel->setObjectName("statusLabel");
    m_statusLabel->setAlignment(Qt::AlignCenter);

    // 进度条
    m_progressBar = new QProgressBar();
    m_progressBar->setObjectName("progressBar");
    m_progressBar->setTextVisible(false);
    m_progressBar->setFixedHeight(12);

    // 番茄计数
    m_tomatoCountLabel = new QLabel("今日完成: " + QString::number(m_completedTomatoes) + "/10");
    m_tomatoCountLabel->setObjectName("tomatoCountLabel");
    m_tomatoCountLabel->setAlignment(Qt::AlignCenter);

    // 每日目标
    m_dailyGoalLabel = new QLabel("每日目标: 10个番茄");
    m_dailyGoalLabel->setObjectName("dailyGoalLabel");
    m_dailyGoalLabel->setAlignment(Qt::AlignCenter);

    // 按钮布局
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(15);

    m_startButton = new QPushButton("开始");
    m_pauseButton = new QPushButton("暂停");
    m_resetButton = new QPushButton("重置");
    m_completeButton = new QPushButton("完成番茄");
    m_CustomTimerButton=new QPushButton("自定义番茄钟");
    m_CustomNewDayButton=new QPushButton("新的一天");

    m_startButton->setObjectName("startButton");
    m_pauseButton->setObjectName("pauseButton");
    m_resetButton->setObjectName("resetButton");
    m_completeButton->setObjectName("completeButton");

    //m_pauseButton->setEnabled(false);
    //m_completeButton->setEnabled(false);

    buttonLayout->addWidget(m_startButton);
    buttonLayout->addWidget(m_pauseButton);
    buttonLayout->addWidget(m_resetButton);
    buttonLayout->addWidget(m_completeButton);
    buttonLayout->addWidget(m_CustomTimerButton);
    buttonLayout->addWidget(m_CustomNewDayButton);
    // 添加到主布局
    mainLayout->addWidget(titleLabel);
    mainLayout->addStretch(1);
    mainLayout->addWidget(m_timeLabel);
    mainLayout->addWidget(m_statusLabel);
    mainLayout->addWidget(m_progressBar);
    mainLayout->addStretch(1);
    mainLayout->addWidget(m_tomatoCountLabel);
    mainLayout->addWidget(m_dailyGoalLabel);
    mainLayout->addStretch(1);
    mainLayout->addLayout(buttonLayout);

    // 连接信号槽
    connect(m_startButton, &QPushButton::clicked, this, &TomatoClockWidget::startTimer);
    connect(m_pauseButton, &QPushButton::clicked, this, &TomatoClockWidget::pauseTimer);
    connect(m_resetButton, &QPushButton::clicked, this, &TomatoClockWidget::resetTimer);
    connect(m_completeButton, &QPushButton::clicked, this, &TomatoClockWidget::completeTomato);
    connect(m_CustomTimerButton, &QPushButton::clicked, this, &TomatoClockWidget::CustomTimer);
    connect(m_CustomNewDayButton, &QPushButton::clicked, [this] {
        toolBox::getInstance().softwareConfiger.simpleSaveConfig("tomato_goal", "0");
        update();
        });
}

void TomatoClockWidget::setupAnimations()
{
    // 进度条动画
    m_progressAnimation = new QPropertyAnimation(this, "progressValue");
    m_progressAnimation->setDuration(300);

    // 脉冲动画效果
    m_pulseEffect = new QGraphicsOpacityEffect(m_timeLabel);
    m_timeLabel->setGraphicsEffect(m_pulseEffect);
    m_pulseAnimation = new QPropertyAnimation(m_pulseEffect, "opacity");
    m_pulseAnimation->setDuration(1000);
    m_pulseAnimation->setStartValue(1.0);
    m_pulseAnimation->setEndValue(0.7);
    m_pulseAnimation->setLoopCount(-1);
}

void TomatoClockWidget::setProgressValue(int value)
{
    m_progressValue = value;
    m_progressBar->setValue(value);
}

void TomatoClockWidget::startTimer()
{
    if (!m_timer->isActive()) {
       
        m_startButton->setEnabled(false);
        m_pauseButton->setEnabled(true);
        m_completeButton->setEnabled(true);
        m_pulseAnimation->start();
    }
}

void TomatoClockWidget::pauseTimer()
{
    is_contiune=!is_contiune;
    if (is_contiune)
    {
        m_pauseButton->setText("暂停");
        m_timer->start(1000);
    }
    else
    {
        m_pauseButton->setText("继续");
        m_timer->stop();
    }
   
}

void TomatoClockWidget::resetTimer()
{
    m_timer->stop();
    m_isWorking = true;

    m_timeLabel->setText("45:00");
 /*   m_startButton->setEnabled(true);
    m_pauseButton->setEnabled(false);
    m_completeButton->setEnabled(false);*/
    m_timer->start(1000);
    m_progressAnimation->setStartValue(0);
    m_progressAnimation->setEndValue(100);
    m_progressAnimation->start();

    // 重新设置脉冲效果
    m_timeLabel->setGraphicsEffect(m_pulseEffect);
    m_pulseAnimation->stop();
    m_pulseEffect->setOpacity(1.0);
}

void TomatoClockWidget::completeTomato()
{
    //结束番茄记录在本地数据库
    //m_db->Write();

    // 完成番茄计数
    m_completedTomatoes++;
    toolBox::getInstance().softwareConfiger.simpleSaveConfig("tomato_goal", QString::number(m_completedTomatoes));
    if (m_completedTomatoes >= 10) {
      
        m_completedTomatoes = 10;
        m_tomatoCountLabel->setText("🎉 今日目标已完成!");
    }
    else {
        m_tomatoCountLabel->setText(QString("今日完成: %1/10").arg(m_completedTomatoes));
    }
}

//自定义时间设置
void TomatoClockWidget::CustomTimer()
{
    TomatoConfigDialog config;
    bool ok = config.exec();
    if (ok) {
        CustomWorkTimer= QTime(0, config.getWorkDuration(), 0);
        CustomRestTimer= QTime(0, config.getRestDuration(), 0);
        m_remainingTime = !m_isWorking ? CustomWorkTimer : CustomRestTimer;
        m_timeLabel->setText(m_remainingTime.toString("mm:ss"));
    }
}

void TomatoClockWidget::updateTimer()
{
    m_remainingTime = m_remainingTime.addSecs(-1);
    m_timeLabel->setText(m_remainingTime.toString("mm:ss"));

    // 检查时间是否结束
    if (m_remainingTime == QTime(0, 0, 0)) {
        m_timer->stop();
        m_isWorking = !m_isWorking;
        m_statusLabel->setText(m_isWorking ? "工作时间" : "休息时间");
        //设置标签的背景颜色蓝色
        m_statusLabel->setStyleSheet(m_isWorking ? "background-color: blue;" : "background-color: green;");

        //弹出任务栏提示
          // 显示消息提示
        if (m_isWorking) {
            m_trayIcon->showMessage("番茄钟提醒", "工作时间结束，请休息", QSystemTrayIcon::Information, 5000);
        }
        else {
            m_trayIcon->showMessage("番茄钟提醒", "休息时间结束，请工作", QSystemTrayIcon::Information, 5000);
        }
        m_remainingTime = m_isWorking ? CustomWorkTimer : CustomRestTimer;
        m_timer->start(1000);
        completeTomato();
    }
}